﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LogicalTree
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindow();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TreeViewItem item = new TreeViewItem() { Header = "逻辑树根" };
            LogicTree(item, this);
            _TreeView.Items.Add(item);
        }

        public void LogicTree(TreeViewItem item, object element)
        {
            if (!(element is DependencyObject)) return;
            TreeViewItem treeViewItem = new TreeViewItem { Header = element.GetType().Name };
            item.Items.Add(treeViewItem);

            var elements = LogicalTreeHelper.GetChildren(element as DependencyObject);
            foreach (object child in elements)
            {
                LogicTree(treeViewItem, child);
            }
        }
    }
}
